package tests;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;

public class TestCreator {
	//number of Figures to be created
	int numberOfFigures;
	//Identification for the tests
	String testID;
	//Used to visualize the input as a matrix
	int[][] initField;
	
	//How big is the area at which the figures will be put
	int fieldSize;
	
	public TestCreator(String iD, int number, int fieldSize) {
		this.numberOfFigures = number;
		
		this.testID = iD;
		
		this.fieldSize = fieldSize;
		
		this.initField = new int[500][500];
	}
	
	public static void main(String[] args) throws IOException {
		Scanner reader = new Scanner(System.in);
		
		System.out.println("Enter ID for the Test:");
		String testID = reader.nextLine();
		
		System.out.println("Enter the number of figures:");
		int numberOfFigures = Integer.parseInt(reader.nextLine());
		
		System.out.println("Enter size of the input area for the figures:");
		int filedSize = Integer.parseInt(reader.nextLine());
		
		reader.close();
		
		TestCreator testCreator = new TestCreator(testID, numberOfFigures, filedSize);
		
		//Create a file with the input
		testCreator.createATest();
		
		//Create a file with the initial Field
		testCreator.createInitField();
	}

	private void createATest() throws IOException {
		
		BufferedWriter writer = new BufferedWriter(new FileWriter(new File("tests/Mivki_" + this.testID + "_Test.txt")));
		
		writer.write(this.numberOfFigures + "\n");
		
		Random rnd = new Random();
		int typeOfFigure;
		int posX;
		int posY;
		String figureName = "";
		for (int i = 0; i < numberOfFigures; i++) {
			//Get a random number for a type of Figure
			typeOfFigure = rnd.nextInt(8);
			
			posX = 1 + rnd.nextInt(this.fieldSize);
			posY = 1 + rnd.nextInt(this.fieldSize);
			
			//Add the new Figure in the local Field
			//Used to display the input in a matrix format
			this.addToInitField(typeOfFigure + 1, posX, posY);
			
			//Decide which the random figure is
			switch(typeOfFigure) {
				case(0): {
					figureName = "ninetile";
					break;
				}
				case(1): {
					figureName = "plus";
					break;
				}
				case(2): {
					figureName = "hline";
					break;
				}
				case(3): {
					figureName = "vline";
					break;
				}
				case(4): {
					figureName = "angle-ur";
					break;
				}
				case(5): {
					figureName = "angle-ul";
					break;
				}
				case(6): {
					figureName = "angle-dr";
					break;
				}
				case(7): {
					figureName = "angle-dl";
					break;
				}
			}
			//Write the new figure to the file
			writer.write(figureName + " " + posX + " " + posY + "\n");
		}
		
		//Closing the writer
		writer.close();
	}
	
	//Add to the initialField
	private void addToInitField(int typeOfFigure, int posX, int posY) {
		switch(typeOfFigure) {
			case(1):
			{
				this.initField[posX][posY] = typeOfFigure;
				this.initField[posX][posY-1] = typeOfFigure;
				this.initField[posX-1][posY-1] = typeOfFigure;
				this.initField[posX-1][posY] = typeOfFigure;
				this.initField[posX-1][posY+1] = typeOfFigure;
				this.initField[posX][posY+1] = typeOfFigure;
				this.initField[posX+1][posY+1] = typeOfFigure;
				this.initField[posX+1][posY] = typeOfFigure;
				this.initField[posX+1][posY-1] = typeOfFigure;
				break;
			}
			case(2):
			{
				this.initField[posX][posY] = typeOfFigure;
				this.initField[posX][posY-1] = typeOfFigure;
				this.initField[posX][posY+1] = typeOfFigure;
				this.initField[posX-1][posY] = typeOfFigure;
				this.initField[posX+1][posY] = typeOfFigure;
				break;
			}
			case(3):
			{
				this.initField[posX][posY] = typeOfFigure;
				this.initField[posX][posY-1] = typeOfFigure;
				this.initField[posX][posY+1] = typeOfFigure;
				break;
			}
			case(4):
			{
				this.initField[posX][posY] = typeOfFigure;
				this.initField[posX-1][posY] = typeOfFigure;
				this.initField[posX+1][posY] = typeOfFigure;
				break;
			}
			case(5):
			{
				this.initField[posX][posY] = typeOfFigure;
				this.initField[posX-1][posY] = typeOfFigure;
				this.initField[posX][posY+1] = typeOfFigure;
				break;
			}
			case(6):
			{
				this.initField[posX][posY] = typeOfFigure;
				this.initField[posX+1][posY] = typeOfFigure;
				this.initField[posX][posY+1] = typeOfFigure;
				break;
			}
			case(7):
			{
				this.initField[posX][posY] = typeOfFigure;
				this.initField[posX-1][posY] = typeOfFigure;
				this.initField[posX][posY-1] = typeOfFigure;
				break;
			}
			case(8):
			{
				this.initField[posX][posY] = typeOfFigure;
				this.initField[posX+1][posY] = typeOfFigure;
				this.initField[posX][posY-1] = typeOfFigure;
				break;
			}
		}		
	}

	private void createInitField() throws IOException {
		BufferedWriter writer = new BufferedWriter(new FileWriter(new File("D:/Mivki_" + this.testID + "_InitialField.txt")));
		
		for (int i = 0; i < this.initField[0].length; i++) {
			for (int j = 0; j < this.initField[0].length; j++) {
				/*
				switch(initField[i][j]) {
					case(0): {
						writer.write("_");
						break;
					}
					case(1): {
						writer.write("D");
						break;
					}
					case(2): {
						writer.write("+");
						break;
					}
					case(3): {
						writer.write("=");
						break;
					}
					case(4): {
						writer.write("|");
						break;
					}
					case(5): {
						writer.write("ur");
						break;
					}
					case(6): {
						writer.write("ul");
						break;
					}
					case(7): {
						writer.write("dr");
						break;
					}
					case(8): {
						writer.write("dl");
						break;
					}
				}
				*/
				writer.write("" + this.initField[i][j]);
			}
			writer.write("\n");
		}
		writer.close();
	}
}
